package com.bfly.mall.user.service.impl;

import com.bfly.core.base.service.BaseServiceImpl;
import com.bfly.core.enums.SysError;
import com.bfly.core.exception.ServiceResponseException;
import com.bfly.mall.user.dao.UserMapper;
import com.bfly.mall.user.dto.UserFormDTO;
import com.bfly.mall.user.entity.User;
import com.bfly.mall.user.service.IUserService;
import com.bfly.mall.wechat.mp.dto.WeChatMpUserInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author 长沙商脉科技有限公司
 * @date 2020/12/24 13:09
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User, Integer> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void activeUser(int userId, boolean enable) {
        User user = get(userId);
        if (user == null) {
            throw new ServiceResponseException(SysError.PARAM_ERROR, "不存在的用户信息!");
        }
        user.setEnable(enable);
        super.edit(user);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(UserFormDTO formDTO) {
        User user = get(formDTO.getId());
        if (user == null) {
            throw new ServiceResponseException(SysError.PARAM_ERROR, "不存在的用户信息!");
        }
        BeanUtils.copyProperties(formDTO, user);
        super.edit(user);
    }

    @Override
    public User getUser(String openId) {
        return userMapper.getUserByOpenId(openId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void bindPhone(int userId, String phone) {
        User user = get(userId);
        if (user == null) {
            throw new ServiceResponseException(SysError.PARAM_ERROR, "不存在的用户信息!");
        }
        user.setPhone(phone);
        super.edit(user);
    }

    @Override
    public int getCount(String openId) {
        return userMapper.getCount(openId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void merge(int platformId,WeChatMpUserInfo userInfo) {
        User user = getUser(userInfo.getOpenid());
        if (user == null) {
            // 保存
            user = userInfo.convert();
            user.setPlatformId(platformId);
            user.setCreateDate(new Date());
            super.save(user);
        } else {
            // 编辑
            User dtoUser=userInfo.convert();
            user.setLanguage(dtoUser.getLanguage());
            user.setNickName(dtoUser.getNickName());
            user.setAvatarUrl(dtoUser.getAvatarUrl());
            user.setSubscribeTime(dtoUser.getSubscribeTime());
            user.setGender(dtoUser.getGender());
            user.setCountry(dtoUser.getCountry());
            user.setCity(dtoUser.getCity());
            user.setCountry(dtoUser.getCountry());
            user.setSubscribe(dtoUser.isSubscribe());
            super.edit(user);
        }
    }

}
